namespace TSL.DAL
{
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Text;
    using TSL.Model;

    public class tuandui
    {
        public List<tuiduan> GetListArray(List<tuiduan> list, string username, string regfrom, string begintime, string endtime, string sscid, string flag)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DataSet ds = new DataSet();
            string sql = "";
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_TuanDui_GetModel");
            if (sscid != "")
            {
                sql = sql + "and type in (select id from betmenu where caipiao_id=''" + sscid + "'')";
            }
            db.AddInParameter(dbCommand, "username", DbType.AnsiString, username);
            db.AddInParameter(dbCommand, "regfrom", DbType.AnsiString, regfrom);
            db.AddInParameter(dbCommand, "begintime", DbType.DateTime, Convert.ToDateTime(begintime));
            db.AddInParameter(dbCommand, "endtime", DbType.DateTime, Convert.ToDateTime(endtime));
            db.AddInParameter(dbCommand, "sql", DbType.AnsiString, sql);
            db.AddInParameter(dbCommand, "flag", DbType.AnsiString, flag);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    list.Add(this.ReaderBind(dataReader));
                }
            }
            return list;
        }

        public List<tuiduan> GetListHTXF(List<tuiduan> list, string username, string begintime, string endtime, string sscid, string flag)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DataSet ds = new DataSet();
            string sql = "1=1";
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_TuanDui_HTGetModel");
            if (sscid != "")
            {
                sql = sql + " and type in (select id from betmenu where caipiao_id=$" + sscid + "$)";
            }
            if (username != "")
            {
                sql = sql + " and username='" + username + "'";
            }
            if (begintime != "")
            {
                sql = sql + " and addtime>$" + begintime + "$";
            }
            if (endtime != "")
            {
                sql = sql + " and addtime<$" + endtime + "$";
            }
            db.AddInParameter(dbCommand, "SQl", DbType.AnsiString, sql);
            db.AddInParameter(dbCommand, "flag", DbType.AnsiString, flag);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    list.Add(this.ReaderBindHT(dataReader));
                }
            }
            return list;
        }

        public DataSet GetListUser(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from users");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by id desc");
            return DatabaseFactory.CreateDatabase().ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public tuiduan ReaderBind(IDataReader dataReader)
        {
            tuiduan model = new tuiduan();
            model.id = dataReader["id"].ToString();
            model.username = dataReader["username"].ToString();
            model.userclass = dataReader["userclass"].ToString();
            model.CHS_userclass = dataReader["CHS_userclass"].ToString();
            model.buy_money = Convert.ToDouble(dataReader["buy_money"].ToString());
            model.Ck_money = Convert.ToDouble(dataReader["Ck_money"].ToString());
            model.FD_money = Convert.ToDouble(dataReader["FD_money"].ToString());
            model.FJ_money = Convert.ToDouble(dataReader["FJ_money"].ToString());
            model.QK_money = Convert.ToDouble(dataReader["QK_money"].ToString());
            model.YL_money = Convert.ToDouble(dataReader["YL_money"].ToString());
            return model;
        }

        public tuiduan ReaderBindHT(IDataReader dataReader)
        {
            tuiduan model = new tuiduan();
            model.username = dataReader["username"].ToString();
            model.buy_money = Convert.ToDouble(dataReader["buy_money"].ToString());
            model.Ck_money = Convert.ToDouble(dataReader["Ck_money"].ToString());
            model.FD_money = Convert.ToDouble(dataReader["FD_money"].ToString());
            model.FJ_money = Convert.ToDouble(dataReader["FJ_money"].ToString());
            model.QK_money = Convert.ToDouble(dataReader["QK_money"].ToString());
            model.YL_money = Convert.ToDouble(dataReader["YL_money"].ToString());
            model.ZJ_money = Convert.ToDouble(dataReader["ZJ_money"].ToString());
            model.XY_money = Convert.ToDouble(dataReader["XY_money"].ToString());
            return model;
        }

        public DataSet TuanDui(string username, string regfrom, string beigintime, string endtime, string sscid)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DataSet ds = new DataSet();
            string sql = "";
            DbCommand dbCommand = db.GetStoredProcCommand("SYS_TuanDui_GetModel");
            if ((beigintime != "") && (endtime != ""))
            {
                sql = "and beigintime>" + beigintime + " and endtime<" + endtime;
            }
            if (sscid != "")
            {
                sql = sql + "and type in (select caipiao_name from betmenu where caipiao_id='" + sscid + "')";
            }
            db.AddInParameter(dbCommand, "username", DbType.AnsiString, username);
            db.AddInParameter(dbCommand, "regfrom", DbType.AnsiString, regfrom);
            db.AddInParameter(dbCommand, "sql", DbType.AnsiString, sql);
            return db.ExecuteDataSet(dbCommand);
        }
    }
}

